package tk.djcrazy.instantmessage.action;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.domain.FriendGroup;
import tk.djcrazy.instantmessage.domain.LoginState;
import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IUserService;
import tk.djcrazy.instantmessage.service.IUserStateService;

import com.opensymphony.xwork2.ActionSupport;

public class UpdateOnLineStateAction extends ActionSupport implements SessionAware{
 
	private static final long serialVersionUID = 4089739524066051759L;

 	private IUserStateService userStateService;
 	private IUserService userService;
 	private UserEntity entity;
 	private Map<String,	Object> session;
 	public String execute() {
 		try {
  		userStateService.updateActiveTime((Integer)session.get("uid"));
	    entity = userService.findById((Integer)session.get("uid"));
		for (FriendGroup group : entity.getGroups()) {
			for (UserEntity user: group.getContentUsers()) {
 				LoginState state = userStateService.getState(user.getId());
 				user.setLoginState(state);
 			}
		}
 		}catch (Exception e) {
 			e.printStackTrace();
 			return ERROR;
 		} 
		return SUCCESS;
	}
	public IUserStateService getUserStateService() {
		return userStateService;
	}
	public void setUserStateService(IUserStateService userStateService) {
		this.userStateService = userStateService;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public UserEntity getEntity() {
		return entity;
	}
	public void setEntity(UserEntity entity) {
		this.entity = entity;
	}
}
